/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of UniCollatorPerseus.
 *
 * OcroPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * OcroPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with OcroPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.ocr.image;

import eu.himeros.util.IoManager;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * Identify glyphs.
 * Main Class.<br/>
 * uses: IoManager, GlyphTokenizer<br/>
 * Pipe with CombineTrainingAndText: GlyphIdentifier --> out.log --> CombineTrainingAndText.<br/>
 *
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class GlyphIdentifier {
    BufferedImage buffImg=null;
    BufferedImage buffOrig=null;
    BufferedImage buffLine=null;
    BufferedImage buffGlyph=null;
    BufferedWriter bw=null;
    Image img=null;
    Toolkit tk=null;
    MediaTracker mt=null;
    Graphics2D g2d=null;
    Graphics2D g2dOrig=null;
    GlyphTokenizer gt=null;
    //TODO lines coords are hard coded on ath-100.png!!! division in lines MUST me made in a different way!!!
    int[] startingPoints={76,103,130,155,182,209,235,263,289,316,343,370,396,423,450,476,502,529,557,584,611,637,663,690,717,744,770,796,824};
    int x=58;
    int y=0;
    int w=518;
    int w1=513;
    int h=23;
    int glyphBestW=13; //best Width for the glyph: it's better to split here!
    int leftPen=10; //max val of penalty on the most left side of the glyph
    int chngPoint=8; //where penalty stop decreasing ans start increasing

    /**
     * Default constructor.
     */
    public GlyphIdentifier(){}

    /**
     *
     * @param args
     */
    protected void init(String[] args){
        bw=IoManager.getWriter(args[2]);
        gt=new GlyphTokenizer(bw);
      //!!!
        tk=Toolkit.getDefaultToolkit();
        img=tk.getImage(args[0]);
        img=(new ImageIcon(img)).getImage();
        buffOrig=new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
        g2dOrig=buffOrig.createGraphics();
        g2dOrig.drawImage(img, 0, 0,null);
        buffImg=new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB); // ???
        for(int i=0;i<startingPoints.length;i++){
            y=startingPoints[i];
            buffLine=buffOrig.getSubimage(x,y,w,h);
            gt.setImageParameters(img.getWidth(null), img.getHeight(null), buffLine.getWidth(), buffLine.getHeight(), glyphBestW, leftPen, chngPoint);
            gt.tokenize(buffLine,x,y);
            int[][] coords=gt.getCoords();
            for(int j=0;j<coords.length;j++){
                int x1=coords[j][0];
                int y1=buffImg.getHeight()-coords[j][1];
                int x2=coords[j][2];
                int y2=buffImg.getHeight()-coords[j][3];
                g2dOrig.setColor(Color.RED);
                g2dOrig.drawRect(x1, y1,x2-x1,y2-y1);
                System.out.println("x1="+x1+"; y1="+y1+"; x2-x1="+(x2-x1)+"; y2-y1="+(y2-y1));
                g2dOrig.setColor(Color.BLACK);
            }
        }
        try{
            ImageIO.write(buffOrig,"png", new File(args[1]));
            bw.close();
        }catch(Exception e){e.printStackTrace();}
    }

    /**
     *
     * @param args
     */
    public static void main(String[]args){
        if(args==null||args.length==0){
            System.out.println("It identifies glyphs inside images");
            System.out.println("Usage eu.himeros.ocrtrainer.GlyphIdentifier <image.png> <boxImage.png> <outputFile>");
            System.out.println("<image.png> is the original image");
            System.out.println("<boxImage.pn> is the image with boxes surrounding glyphs");
            System.out.println("<outputFile> is the result");
            System.exit(1);
        }
        GlyphIdentifier gi=new GlyphIdentifier();
        gi.init(args); 
    }

}
